#ifndef _STERI_CTR_H_
#define _STERI_CTR_H_

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "iohandle.h"
#include "rtctime.h"
#include "pid_tune.h"
#include "simp_pid.h"
#include "sys_init.h"
#include "mytype.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define STRI_REQ_AUTO 0
#define STRI_REQ_ASSIGN 1

#define STRI_F0_SEL_AIR 0 
#define STRI_F0_SEL_OBJ 1 

#define STRI_PROG_MODE
	typedef enum
	{
		STRI_PROG_MODE_NONE = 0, 
		STRI_PROG_MODE_PROG		 
	} stri_prog_mode_type;

#define STRI_PRESS_DUAL

	typedef enum
	{
		STRI_PRESS_DUAL_NONE = 0,  
		STRI_PRESS_DUAL_PULSE,	   
		STRI_PRESS_DUAL_SATURATED, 
		STRI_PRESS_DUAL_VACUUM,	   
	} stri_press_dual_type;

#define STRI_WATO_MODE

	typedef enum
	{
		STRI_WATO_MODE_NONE = 0, 
		STRI_WATO_MODE_STEAM,	 
		STRI_WATO_MODE_TIME,	 
		STRI_WATO_MODE_EXT		 
	} stri_water_out_type;

#define STRI_DRY_MODE

	typedef enum
	{
		STRI_DRY_MODE_NONE = 0, 
		STRI_DRY_MODE_HEAT,		
		STRI_DRY_MODE_AIR_IN,	
		STRI_DRY_MODE_VACUUM,	
	} stri_dry_mode_type;

#define DRY_STEP
	typedef enum
	{
		DRY_STEP_PREV = 0,
		DRY_STEP_DLY,
		DRY_STEP_DRY,
		DRY_STEP_COMP,
		DRY_STEP_END = 50,
	} stri_dry_step_type;

#define STRI_WARM_MODE

	typedef enum
	{
		STRI_WARM_MODE_NONE = 0,
		STRI_WARM_MODE_STEAM, 
		STRI_WARM_MODE_EXTHT, 
	} stri_warm_mode_type;

#define STRI_WARM_MODE

	typedef enum
	{
		STRI_AIROUT_MODE_LEVEL = 0, 
		STRI_AIROUT_MODE_PER = 1,	
	} stri_airout_mode_type;

#define STRI_MAIN_HEAT_MODE
	typedef enum
	{
		STRI_MAIN_HEAT_AIR = 0,

		STRI_MAIN_HEAT_PRESS,

		STRI_MAIN_HEAT_OBJ_AVG,

		STRI_MAIN_HEAT_OBJ_MIN,

		STRI_MAIN_HEAT_OBJ_SURE,

		STRI_MAIN_HEAT_OBJ_CASCADE,
	} stri_main_heat_mode_type;

	typedef struct
	{
		ii16 temp;
		ii16 time;
	} stri_porg_type;

	typedef enum
	{
		STRI_STOP_NONE = 0,
		STRI_STOP_KEY = 1,		 
		STRI_STOP_WAT = 2,		 
		STRI_STOP_GAO_YA = 3,	 
		STRI_STOP_GAN_SHAO = 4,	 
		STRI_STOP_DOOR_LOCK = 5, 
		STRI_STOP_ELEC_LOCK = 6, 
		STRI_STOP_DOOR_OPEN = 7, 
	} stri_stop_type;

	typedef enum
	{
		STRI_STEP_IDLE = 0,	  
		STRI_STEP_CHECK,	  
		STRI_STEP_ORDER_TIME, 
		STRI_STEP_ORDER_HEAT, 
		STRI_STEP_PRESSURIZE, 
		STRI_STEP_VACUUM,	  
		STRI_STEP_PRE_VACUUM, 
		STRI_STEP_PRE_HEAT,	  
		STRI_STEP_HEAT,		  
		STRI_STEP_DELAY,	  
		STRI_STEP_PRESS_DO,	  
		STRI_STEP_PRESS_UP,	  
		STRI_STEP_PRESS_KEEP, 
		STRI_STEP_AUTO_COOL,  
		STRI_STEP_AIROUT,	  
		STRI_STEP_WATOUT,	  
		STRI_STEP_DRY,		  
		STRI_STEP_COMP_COOL,  
		STRI_STEP_WARM,		  
		STRI_STEP_COMP,		  
		STRI_STEP_END,		  
	} stri_step_type;

	typedef struct
	{
		lib_rs_req_type rs_cmd; 

		struct
		{
			ii16 air, obj, ext, press;			
			float f_air, f_obj, f_ext, f_press; 

			ii16 air_list[32]; 
			ii16 obj_list[32]; 
			ii16 ext_list[32]; 
			uu8 list_index;	   
			uu8 list_time_cnt;

			ii16 order;	 
			ii16 ft;	 
			ii16 offset; 
			ii16 max;	 
			ii16 above;	 
		} temp;

		struct
		{
			ii16 temp; 
			ii16 time; 
		} stri;

		struct
		{
			stri_prog_mode_type mode; 
			uu8 seg_max;			  
			uu8 seg_cnt;			  
			stri_porg_type *tab;	  
		} prog;

		struct
		{
			uu8 en : 1;	 
			float cs_up; 
			float cs_dn; 
		} kl;

		struct
		{
			ii16 temp;				 
			ii16 time;				 
			stri_dry_mode_type mode; 
			stri_dry_step_type step; 
		} dry;

		struct
		{
			stri_warm_mode_type mode; 
			ii16 temp;				  
			ii16 time;				  
		} warm;

		struct
		{
			ii16 target_temp;		   
			ii16 relieve_temp;		   
			ii16 press_offset;		   
			ii16 vaccum;			   
			stri_press_dual_type mode; 
			uu8 sv;					   
			uu8 airout_min;			   
			uu16 tup_airout_interval;  
			uu16 tdn_airout_interval;  
			uu8 tup_airout_time;	   
			uu8 tdn_airout_time;	   

			uu8 cnt;		  
			uu8 step;		  
			uu8 saturated_ok; 
		} press_dual;

		struct
		{
			uu8 ot;			 
			uu8 dly;		 
			wt_add_type obj; 
		} wat_add;

		struct
		{
			ii16 start_temp;		  
			ii16 end_temp;			  
			ii16 time;				  
			stri_water_out_type mode; 
			uu8 end_delay;			  
		} wat_out;

		struct
		{
			ii16 buchang_press;	 
			ii16 buchang_offset; 
			uu8 check_step;		 
			uu8 check_cnt;		 
			uu8 check_ok : 1;	 
			uu8 check_en : 1;	 
			uu8 buchang_en : 1;	 
			ii16 check_val;		 
			ii16 check_min;		 
			ii16 check_time;	 
			ii16 timeout;		 
		} pressurize;

		struct
		{
			uu8 check_en : 1; 
			uu8 fore_en : 1;  
			uu8 check_ok : 1; 
			uu8 check_step;	  
			uu8 check_cnt;	  
			ii16 check_val;	  
			ii16 check_max;	  
			ii16 check_time;  
			ii16 timeout;	  
		} vacuum;

		struct
		{
			uu08 st_delay_run; 
			uu08 st_delay_end; 
			ii16 temp_offset;  
			ii16 time;		   
		} press_up_delay;

		struct
		{
			stri_airout_mode_type mode;
			uu8 level;			 
			ii16 micro_out_temp; 
			ii16 full_out_temp;	 
		} airout;

		struct
		{
			ii16 heat_delay_time; 
			ii16 temp_open;		  
			ii16 press_open;	  

			ii16 order_time_dly;			
			micro_time_type order_time_rtc; 
			ii16 order_heat_timeout_sv;		

			uu8 order_heat_en : 1;	
			uu8 fan_auto_start : 1; 
			uu8 f0_sel : 1;			
		} config;

		struct
		{
			uu8 t_obj_en : 1;	   
			uu8 t_ext_en : 1;	   
			uu8 press_en : 1;	   
			uu8 vaccum_en : 1;	   
			uu8 pressurize_en : 1; 
			uu8 air_in_en : 1;	   
			uu8 wat_add_en : 1;	   
			uu8 wat_out_en : 1;	   

			ii16 airout_time;		  
			ii16 airout_interval_min; 
			ii16 airout_level_max;	  
			ii16 airout_period_max;	  
			uu8 dry_start_delay;	  
			ii16 dry_vacuum_low;	  
			ii16 dry_vacuum_high;	  
			ii16 wat_add_dis_temp;	  
			ii16 saturated_delay;	  
			ii16 press_pulse_delay;	  
		} hal;

		struct
		{
			uu8 first;
			stri_step_type now;
			stri_step_type req;
			ii32 time_cnt; 
		} step;

		struct
		{
			int32_t air_out_en_time;		
			int32_t air_out_dis_time;		
			ii32 sub_time_cnt;				
			ii32 total_time_cnt;			
			ii32 heat_time_cnt;				
			float f0_stri, f0_proc;			
			float air_f0_stri, air_f0_proc; 
			float obj_f0_stri, obj_f0_proc; 
			uu8 f0_time_cnt;				
			out_sw_type airout;				
			stri_stop_type stop_src;		
			char sucess : 1;				
			char wat_add_allow : 1;			
			char ext_heat_comp : 1;			
			char stop_req : 1;				
		} state;

		struct
		{
			micro_time_type rtc; 
			ii16 loop_cnt;		 
			
			char wat_low : 1;	  
			char wat_high : 1;	  
			char ext_err : 1;	  
			char pulse_100ms : 1; 
		} mix;

		struct
		{
			char main_en : 1, ext_en : 1;								  
			char pid_cal_pulse : 1, pid_tune_pulse : 1;					  
			stri_main_heat_mode_type main_mode_set;						  
			stri_main_heat_mode_type main_mode_now, main_mode_old;		  
			ii16 ext_main_offset;										  
			ii16 heat_period;											  
			ii16 ext_heat_duty, main_heat_duty;							  
			uint8_t st_air_tune_comp, st_ext_tune_comp, st_obj_tune_comp; 
			ii08 tune_req;												  
			simp_pid_type air_pid, ext_pid, obj_pid;					  
			pid_tune_type tune;											  
			float main_sv;												  
			float ext_sv;												  
		} heat;

		struct
		{
			char air_out;		  
			char wat_out : 1;	  
			char wat_add : 1;	  
			char air_in : 1;	  
			char pressurize : 1;  
			char vacuum : 1;	  
			char fan_en_req : 1;  
			char fan_dis_req : 1; 
			char unlock : 1;	  
		} out;

		union
		{
			uu16 val;

			struct
			{
				uu16 vaccum_timeout : 1;	 
				uu16 vaccum_check : 1;		 
				uu16 pressurize_timeout : 1; 
				uu16 pressurize_check : 1;	 
				uu16 lack_water : 1;		 
			} bits;

		} error;

	} stri_type;

	typedef enum
	{
		STRI_FUN_NOP = 0,
		STRI_FUN_EXT = 1,
		STRI_FUN_ORR = 2,
	} steri_fun_type;

	typedef struct
	{
		steri_fun_type mode;
		char (*fun)(void);
	} stri_fun_type;

	typedef struct
	{
		stri_fun_type idle;
		stri_fun_type check_self;
		stri_fun_type order_time;
		stri_fun_type order_heat;
		stri_fun_type pressurize;
		stri_fun_type vacuum;
		stri_fun_type for_vacuum;
		stri_fun_type pre_heat;
		stri_fun_type heat;
		stri_fun_type heat_delay;
		stri_fun_type press_dual;
		stri_fun_type press_up;
		stri_fun_type press_keep;
		stri_fun_type auto_cool;
		stri_fun_type air_out;
		stri_fun_type wat_out;
		stri_fun_type dry;
		stri_fun_type warm;
		stri_fun_type comp_cool;
		stri_fun_type comp;
		stri_fun_type end;
	} stri_fun_list_type;

	typedef struct
	{
		uu08 flag : 4;			
		uu08 obj_temp_en : 1;	
		uu08 ext_heat_en : 1;	
		uu08 press_en : 1;		
		uu08 f0_en : 1;			
		uu08 cool_en : 1;		
		uu08 press_buchang : 1; 
		uu08 xie_lou_check : 1; 
		uu08 heat_order_en : 1; 
		uu08 warm_en : 1;		
		uu08 dry_en : 1;		
		uu08 pulse_num;			
		uu08 prog_type;			
		uu08 prog_num;			
		uu08 user_sel;			
		ii16 f0_set;			
		ii16 stri_temp;			
		ii16 stri_time;			
		ii16 air_out_temp;		
		ii16 open_temp;			
		ii16 warm_temp;			
		ii16 dry_temp;			
		ii16 warm_time;			
		ii16 dry_time;			
		ii16 stri_cnt;			
		ii16 order_temp;		
		
		char proc_name[24]; 
		char user_name[24]; 
	} stri_hist_head_type;

	typedef struct
	{
		uu08 flag : 4;	  
		uu08 st_pt : 1;	  
		uu08 st_line : 1; 
		uu08 st_sec : 1;  
		uu08 step;		  
		ii16 air_temp;	  
		ii16 obj_temp;	  
		ii16 press;		  
	} stri_hist_mid_type;

	typedef struct
	{
		uu08 flag : 4;				   
		uu08 stri_res : 1;			   
		ii16 f0_stri, f0_proc;		   
		ii16 air_f0_stri, air_f0_proc; 
		ii16 obj_f0_stri, obj_f0_proc; 
	} stri_hist_tail_type;

	typedef union
	{
		stri_hist_head_type head;
		stri_hist_mid_type mid;
		stri_hist_tail_type tail;
	} stri_hist_type;

	void stri_interval_air_out(void);

	void stri_start(void);
	void stri_stop(void);

	char stri_st_set_at_start(void);
	char stri_step_req(stri_step_type req, uu8 mode);
	void stri_heat_main(void);
	void stri_heat_ext(void);
	void stri_config(void);
	void stri_rt_update(void);
	void stri_wat_add_handle(void);
	void stri_main_heat_ctr(char ctr, float sv);
	void stri_ext_heat_ctr(char ctr, float sv);
	void stri_hal_default(void);

#define stri_list_index(x) ((x) & 0x1F)

	void stri_control(void);

	extern stri_type stri;
	extern const stri_fun_list_type stri_fun;

	extern const short stri_airout_level[]; 

#ifdef __cplusplus
}
#endif

#endif 

